layui.use(['table', 'form', 'laydate'], function() {
	var table = layui.table;
	var form = layui.form;
	var laydate = layui.laydate;
	table.render({
		elem: '#adminTeacher',
		url: 'http://localhost:8080/springend-sys/getAllListTeacher',
		toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
		cellMinWidth: 80,
		cols: [
			[{
				type: 'checkbox'
			}, {
				field: 'tno',
				title: '教师工号',
				width: 100,
				unresize: true,
			}, {
				field: 'tname',
				title: '姓名',
			}, {
				field: 'deptname',
				title: '系别',
				width: 130,
			}, {
				field: 'createtime',
				title: '入职时间',
				width: 130,
			}, {
				field: 'level',
				title: '级别',
			}, {
				field: 'sex',
				title: '性别',
			}, {
				field: 'phone',
				title: '手机号',
				width: 140
			}, {
				field: 'birthday',
				title: '生日',
				width: 130,
			}, {
				field: 'lock',
				title: '是否锁定',
				width: 110,
				templet: '#checkboxTpl',
				unresize: false,
				templet: function(obj) {
					if (obj.lock == 0) {
						return '<input type="checkbox" checked="" name="lock" value="' +
							obj.tid + '" title="锁定" lay-filter="lockDemo">';
					} else if (obj.lock == 1) {
						return '<input type="checkbox" name="lock" value="' + obj.tid +
							'" title="锁定" lay-filter="lockDemo">';
					}
				}
			}, {
				fixed: 'right',
				title: '操作',
				toolbar: '#barDemo',
				width: 200
			}]
		],
		page: true,
		parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
			// console.log(res);
			return {
				"code": res.status, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.count, //解析数据长度
				"data": res.data.list //解析数据列表
			};
		}
	});

	//监听锁定操作
	form.on('checkbox(lockDemo)', function(obj) {
		// layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
		// console.log(obj.value);
		// console.log(obj.elem.checked);
		if (obj.elem.checked == false) {
			// console.log("锁定");
			$.ajax({
				url: "http://localhost:8080/springend-sys/updateTeacher",
				type: "post",
				data: {
					tid: obj.value,
					lock: 1,
				},
				dataType: "json",
				success: function(result) {
					if (result.status == 0) {
						layer.msg("已锁定");
					}
				}
			})
		} else if (obj.elem.checked == true) {
			// console.log("解锁");
			$.ajax({
				url: "http://localhost:8080/springend-sys/updateTeacher",
				type: "post",
				data: {
					tid: obj.value,
					lock: 0,
				},
				dataType: "json",
				success: function(result) {
					if (result.status == 0) {
						layer.msg("已解锁");
					}
				}
			})
		}
	});

	//头工具栏事件
	table.on('toolbar(adminTeacher)', function(obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		switch (obj.event) {
			case 'addData':
				$("#teacherForm")[0].reset();
				layer.open({
					type: 1,
					title: '增加',
					area: ['700px', '380px'],
					offset: ['0%', '20%'],
					btn: ['确认', '取消'],
					btnAlign: 'c',
					content: $("#teacherForm"),
					yes: function(index, layero) {
						$.ajax({
							url: "http://localhost:8080/springend-sys/insertTeacher",
							type: "post",
							data: {
								tid: $("#tid").val(),
								tno: $("#tno").val(),
								tname: $("#tname").val(),
								sex: $("#sex").val(),
								age: $("#age").val(),
								level: $("#level").val(),
								deptid: $("#deptid").val(),
								birthday: $("#birthday").val(),
								email: $("#email").val(),
								phone: $("#phone").val()
							},
							dataType: "json",
							success: function(result) {
								layer.closeAll();
								layer.msg(result.message);
								$("#teacherForm")[0].reset();
								//执行重载
								table.reload('adminTeacher');
							}
						})
					},
					btn2: function(index, layero) {}
				});
				break;
		};
	});

	//监听行工具事件
	table.on('tool(adminTeacher)', function(obj) {
		var data = obj.data;
		console.log(data);
		if (obj.event === 'delData') {
			layer.confirm('真的删除行么', function(index) {
				$.ajax({
					url: "http://localhost:8080/springend-sys/deleteTeacher",
					type: "post",
					data: {
						tid: data.tid
					},
					dataType: "json",
					success: function(result) {
						if (result.status == 0) {
							layer.closeAll();
							layer.msg(result.message);
							table.reload('adminTeacher');
						}
					}
				})
			});
		} else if (obj.event === 'edit') {
			var tno = obj.data.tno;
			form.val("teacherForm", data);
			layer.open({
				type: 1,
				title: '修改',
				area: ['700px', '380px'],
				offset: ['0%', '20%'],
				btn: ['确认', '取消'],
				btnAlign: 'c',
				content: $("#teacherForm"),
				yes: function(index, layero) {
					var teaid = $("#tid").val();
					if (tno != teaid) {
						layer.msg("职工号不能修改");
						return false;
					}
					$.ajax({
						url: "http://localhost:8080/springend-sys/updateTeacher",
						type: "post",
						data: {
							tid: $("#tid").val(),
							tname: $("#tname").val(),
							sex: $("#sex").val(),
							age: $("#age").val(),
							level: $("#level").val(),
							deptid: $("#deptid").val(),
							birthday: $("#birthday").val(),
							email: $("#email").val(),
							phone: $("#phone").val()
						},
						dataType: "json",
						success: function(result) {
							if (result.status == 0) {
								layer.closeAll();
								layer.msg(result.message);
								$("#teacherForm")[0].reset();
								//执行重载
								table.reload('adminTeacher');
							}
						}
					})
				},
				btn2: function(index, layero) {}
			})
		} else if (obj.event === 'detail') {
			form.val("teacherForm", data);
			layer.open({
				type: 1,
				title: '详情',
				area: ['700px', '380px'],
				offset: ['0%', '20%'],
				content: $("#teacherForm")
			})
		}
	});
	//监听提交
	form.on('submit(demo1)', function(data) {
		//执行重载
		table.reload('adminTeacher', {
			where: {
				deptid: $("#Deptid").val(),
				level: $("#Level").val(),
				sex: $("#Sex").val(),
			}
		});
		return false;
	});
	//常规用法
	laydate.render({
		elem: '#birthday'
	});
});
